package ${controllerPackage};

import ${basePackage}.core.Result;
import ${basePackage}.core.ResultGenerator;
import ${basePackage}.exception.ServiceException;
import ${basePackage}.model.${modelNameUpperCamel};
import ${basePackage}.service.${modelNameUpperCamel}Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author ${author}
 * @data ${date}.
 */
@RestController
@RequestMapping("${baseRequestMapping}")
@Api(value = "${modelNameUpperCamel}控制类", tags = "${modelNameUpperCamel}控制类接口测试")
public class ${modelNameUpperCamel}Controller {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Resource
    private ${modelNameUpperCamel}Service ${modelNameLowerCamel}Service;

    @PostMapping("/add")
    @ApiOperation(value = "添加数据",notes = "添加新的数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "${modelNameLowerCamel}",value = "待添加的${modelNameLowerCamel}实例",paramType = "body",dataType = "${modelNameUpperCamel}",required = true)
    })
    public Result add(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
        ${modelNameLowerCamel}Service.save(${modelNameLowerCamel});
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除数据",notes = "根据id删除数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,httpMethod = "DELETE")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "查询的id", paramType = "path",required = true,dataType = "Integer"),
    })
    public Result delete(@PathVariable Integer id) {
        checkNotNull(id);
        ${modelNameLowerCamel}Service.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新数据",notes = "根据内容更新数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,httpMethod = "PUT")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "${modelNameLowerCamel}",value = "更新的${modelNameLowerCamel}实例",paramType = "body",dataType = "${modelNameUpperCamel}",required = true)
    })
    public Result update(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
        checkNotNull(${modelNameLowerCamel}.getId());
        ${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "获取单个值",notes = "查看单个项目的内容", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "查询的id", paramType = "path",required = true,dataType = "Integer",defaultValue = "0")
    })
    public Result detail(@PathVariable Integer id) {
        checkNotNull(id);
        ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id);
        return ResultGenerator.genSuccessResult(${modelNameLowerCamel});
    }

    @GetMapping("/detailBri/{id}")
    @ApiOperation(value = "获取单个值(简洁接口)",notes = "查看单个项目的内容(只返回需要的数据)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "查询的id", paramType = "path",required = true,dataType = "Integer",defaultValue = "0")
    })
    public Result detailBri(@PathVariable Integer id) {
        checkNotNull(id);
        String sql = "SELECT * FROM ${modelNameLowerCamel} WHERE id=?";
        ${modelNameUpperCamel} ${modelNameLowerCamel} = (${modelNameUpperCamel}) jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper(${modelNameUpperCamel}.class));
        return ResultGenerator.genSuccessResult(${modelNameLowerCamel});
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取全部",notes = "返回分页过后的数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page",value = "查询页码", paramType = "query",required = true,dataType = "Integer",defaultValue = "0"),
        @ApiImplicitParam(name = "size",value = "每页数据量", paramType = "query",required = true,dataType = "Integer",defaultValue = "5")
    })
    public Result <PageInfo<${modelNameUpperCamel}>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        PageHelper.startPage(page, size);
        List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll();
        PageInfo<${modelNameUpperCamel}> pageInfo = new PageInfo<>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/listBri")
    @ApiOperation(value = "获取全部(简洁接口)",notes = "返回分页过后的数据(只返回需要的数据)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page",value = "查询页码", paramType = "query",required = true,dataType = "Integer",defaultValue = "0"),
        @ApiImplicitParam(name = "size",value = "每页数据量", paramType = "query",required = true,dataType = "Integer",defaultValue = "5")
    })
    public Result <List<${modelNameUpperCamel}>> listBri(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        String sql = "SELECT * FROM ${modelNameLowerCamel} LIMIT ?,?";
        List<${modelNameUpperCamel}> list = (List<${modelNameUpperCamel}>) jdbcTemplate.query(sql, new Object[]{size * page, size * (page + 1) - 1}, new BeanPropertyRowMapper<${modelNameUpperCamel}>(${modelNameUpperCamel}.class));
        return ResultGenerator.genSuccessResult(list);
    }

    /*
    * @Description: 判断操作的数据是否存在
    * @Author: brilliantzhao
    * @CreateDate: 2019/5/31 14:03
    * @Param:
    * @Return:
    */
    public void checkNotNull(Integer id){
        String checkSql = "SELECT COUNT(id) FROM ${modelNameLowerCamel} WHERE id=?";
        int checkSqlInt = jdbcTemplate.queryForObject(checkSql, new Object[]{id}, Integer.class);
        if (checkSqlInt == 0) {
            // 不存在
            throw new ServiceException("您要的数据不存在");
        }
    }

}
